package com.tool.tools.view.pages;

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author Maktoub
 * @description
 * @date 2024年12月30日 16:42
 **/
public class TimeStampPage {
    public static void showTimestampConverter(Stage primaryStage, Scene mainScene) {
        // 创建时间戳转换页面
        GridPane timestampConverterLayout = new GridPane();
        timestampConverterLayout.setPadding(new Insets(10));
        timestampConverterLayout.setVgap(8);
        timestampConverterLayout.setHgap(10);

        Label timestampLabel = new Label("输入时间戳:");
        TextField timestampInput = new TextField();
        Button convertTimestampButton = new Button("转换");
        Label convertedTimeLabel = new Label("转换后的时间:");
        TextField convertedTimeOutput = new TextField();
        convertedTimeOutput.setEditable(false);

        Label timeLabel = new Label("输入时间 (yyyy-MM-dd HH:mm:ss):");
        TextField timeInput = new TextField();
        Button convertTimeButton = new Button("转换");
        Label convertedTimestampLabel = new Label("转换后的时间戳:");
        TextField convertedTimestampOutput = new TextField();
        convertedTimestampOutput.setEditable(false);

        Button backButton = new Button("返回");
        backButton.setOnAction(e -> {
            primaryStage.setScene(mainScene); // 切换回主场景
        });

        GridPane.setConstraints(timestampLabel, 0, 0);
        GridPane.setConstraints(timestampInput, 1, 0);
        GridPane.setConstraints(convertTimestampButton, 2, 0);
        GridPane.setConstraints(convertedTimeLabel, 0, 1);
        GridPane.setConstraints(convertedTimeOutput, 1, 1);

        GridPane.setConstraints(timeLabel, 0, 3);
        GridPane.setConstraints(timeInput, 1, 3);
        GridPane.setConstraints(convertTimeButton, 2, 3);
        GridPane.setConstraints(convertedTimestampLabel, 0, 4);
        GridPane.setConstraints(convertedTimestampOutput, 1, 4);

        GridPane.setConstraints(backButton, 0, 5); // 假设放在第5行第0列

        convertTimestampButton.setOnAction(e -> {
            try {
                long timestamp = Long.parseLong(timestampInput.getText());
                LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
                convertedTimeOutput.setText(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            } catch (NumberFormatException ex) {
                convertedTimeOutput.setText("无效的时间戳");
            }
        });

        convertTimeButton.setOnAction(e -> {
            try {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime dateTime = LocalDateTime.parse(timeInput.getText(), formatter);
                long timestamp = dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                convertedTimestampOutput.setText(String.valueOf(timestamp));
            } catch (Exception ex) {
                convertedTimestampOutput.setText("无效的时间格式");
            }
        });

        timestampConverterLayout.getChildren().addAll(
                timestampLabel, timestampInput, convertTimestampButton, convertedTimeLabel, convertedTimeOutput, // 时间戳转换时间
                timeLabel, timeInput, convertTimeButton, convertedTimestampLabel, convertedTimestampOutput, // 时间转换时间戳
                backButton // 添加返回按钮
        );

        Scene timestampConverterScene = new Scene(timestampConverterLayout, 600, 300);
        primaryStage.setScene(timestampConverterScene);
    }
}
